package com.person.jc.service.base.unqualified;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdUnqualifiedService extends Service {

public static final BdUnqualifiedService me = new BdUnqualifiedService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select * from bd_unqualified where status <> 3 ");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("unName")))){
			sql.append(" and un_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("status")))){
			sql.append(" and status = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("unId")))){
			sql.append(" and un_id = '"+ temp +"'");
		}
		sql.append(" order by un_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_unqualified (un_name, status, remark) values (?, ?, ?)";
		Db.update(sql, map.get("unName"), map.get("status"), map.get("remark"));
		super.setCRUD("不合格原因", "C", "新增不合格原因，原因："+map.get("unName"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_unqualified set un_name = ? , status = ?, remark = ? where un_id = ?";
		Db.update(sql, map.get("unName"), map.get("status"), map.get("remark"), map.get("unId"));
		super.setCRUD("不合格原因", "U", "修改不合格原因，编号："+map.get("unId"));
	}
	
	public void delete(String unId){
		String sql = " update bd_unqualified set status = 3 where un_id = ? ";
		Db.update(sql, unId);
		super.setCRUD("不合格原因", "D", "删除不合格原因，编号："+unId);
	}
	
	public long checkName(String unName, String unId){
		String sql = "select count(1) from bd_unqualified where un_name = ? and status <> 3 ";
		if(Utils.isNotEmpty(unId)){
			sql += " and un_id <> " + unId;
		}
		return Db.queryLong(sql, unName);
	}
	
}
